

###################################################
## Replication code for Grewal and Kureshi (JCR) ##
###################################################

## Load packages
library(stargazer)
library(mfx)
library(ggplot2)
library(survival)
library(SurvRegCensCov)
library(mediation)
library(zoo)
library(xtable)

## Load data
data <- read.csv("data.csv")
data2 <- read.csv("dataCY.csv")
data3 <- read.csv("dataCM.csv")


## Figure 1: Which Coup Leaders Hold Elections?

data.summary <- tapply(data$elex[data$powercoup==1], data$IV[data$powercoup==1], mean, na.rm=TRUE)
barplot <- barplot(data.summary, ylim=c(0,1), ylab="Proportion Holding Elections", cex.names=0.9, col="DarkBlue", main="Figure 1: Which Coup Leaders Hold Elections?", names=c("Pr. Autocratic\n + Mil Aid", "Pr. Autocratic\n + No Mil Aid", "Pr. Democratic\n + Mil Aid", "Pr. Democratic\n + No Mil Aid"))
text(x=t(barplot), y=data.summary+0.06, labels=round(data.summary, 2), pos=2, offset=-0.8)




## Table 1: Elections as Coup Legitimation

one <- glm(elex~t+t2+t3+aidlag*predem, data=data[data$powercoup==1,], family=binomial(link=logit))
two <- glm(elex~t+t2+t3+aidlag*predem+gdp+chgdp+gdplag+prchgdp+pop+junior+post90+Oil+preprotest, data=data[data$powercoup==1,], family=binomial(link=logit))
three <- glm(elex~t+t2+t3+aidlag*predem+gdp+chgdp+gdplag+prchgdp+pop+junior+post90+Oil+preprotest+protest+intl_protests_dum, data=data[data$powercoup==1,], family=binomial(link=logit))

stargazer(one, two, three, single.row=TRUE)



## Figure 2: Marginal Effects Plot

logitmfx <- logitmfx(two$formula, two$data, atmean = TRUE, robust = FALSE, clustervar1 = NULL, clustervar2 = NULL, start = NULL, control = list())

logitmfx <- as.data.frame(logitmfx$mfxest)
logitmfx <- logitmfx[-c(1:3),]
logitmfx$V1 <- cbind(c("Pr. Military Aid", "Previously Democratic", "GDP/capita", "Ch. GDP/capita", "Pr. GDP/capita", "Pr. Ch. GDP/capita", "Population", "Junior Officer", "Post-1990", "Oil Exporter", "Pre-Protests", "Pr. Aid*Pr. Democratic"))
logitmfx$me <- logitmfx[,1]
logitmfx$se <- logitmfx[,2]
logitmfx <- logitmfx[c(12,2,1,3,4,5,6,7,8,9,10,11),]
logitmfx$V1 <- factor(logitmfx$V1, levels = logitmfx$V1)

ggplot(logitmfx, aes(V1, me,ymin = me - 2*se,ymax= me + 2*se)) +
  scale_x_discrete(name="") +
  scale_y_continuous(name="",limits=c(-0.8,0.8), breaks=c(-0.8,-0.4,0,0.4,0.8)) +
  theme_bw() + 
  theme(axis.text.y = element_text(size=14),
        axis.text.x = element_text(size=12)) +
  geom_errorbar(aes(x = V1, y = me),size=.3,width=.2) + 
  geom_point(aes(x = V1, y = me)) +
  geom_hline(yintercept=0) + 
  coord_flip() +
  ggtitle("Figure 2: Marginal Effects Plot for Elections")


## Table 2: Durability of Coup Regimes

one <- survreg(Surv(duration+1, event=censor)~aidlag*predem, data=data[data$powercoup==1,], dist="weibull")
two <- survreg(Surv(duration+1, event=censor)~aidlag*predem+elex, data=data[data$powercoup==1,], dist="weibull")
three <- survreg(Surv(duration+1, event=censor)~aidlag*predem+elex_lost+gdp+chgdp+gdplag+prchgdp+pop+junior+post90+Oil+preprotest, data=data[data$powercoup==1,], dist="weibull")
four <- survreg(Surv(duration+1, event=censor)~aidlag*predem+elex_lost+gdp+chgdp+gdplag+prchgdp+pop+junior+post90+Oil+preprotest+elex, data=data[data$powercoup==1,], dist="weibull")

stargazer(one, two, three, four, single.row=TRUE)



## Figure 3: Mediation

test <- na.omit(data[, c("duration", "censor","elex", "aidlag", "predem", "elex_lost", "powercoup", "pop", "gdp", "chgdp", "gdplag", "prchgdp", "junior","post90", "Oil","preprotest", "demaid")])
med.fit <- glm(elex~pop+gdp+chgdp+gdplag+prchgdp+junior+post90+Oil+preprotest+aidlag+predem+demaid, data=test[test$powercoup==1,], family=binomial(link=logit))
summary(med.fit)
out.fit <- survreg(Surv(duration+1, event=censor)~aidlag+predem+demaid+elex_lost+pop+gdp+chgdp+gdplag+prchgdp+junior+post90+Oil+preprotest+elex, data=test[test$powercoup==1,], dist="weibull")
summary(out.fit)

cm <- mediate(med.fit, out.fit, treat="demaid", mediator="elex", outcome="Surv(duration+1, event=censor)", sims=1000)
summary(cm)
plot(cm, main="Figure 3: Effect of Aid*Democracy on Duration\nMediated Through Elections", xlab="Duration (in months)")





##############
## APPENDIX ##
##############

# Table 3: List of coups

data$start <- as.yearmon(data$start)
power <- data[data$powercoup==1,]

xtable(data.frame(coup=paste(paste(power[order(power$country,power$start),]$country, ",", sep=""), power[order(power$country,power$start),]$start[power$powercoup==1])))



## Table 4: Time-Series Set-up

one <- glm(elx~factor(country)+t+t2+t3+pop+junior+post90+Oil+preprotest+gdppc+chgdppc+aidlag*predem, data=data2[data2$powercoup==1,], family=binomial(link=logit))

two <- glm(elx~factor(country)+t+t2+t3+pop+junior+post90+Oil+preprotest+gdppc+chgdppc+duration2+aidlag*predem, data=data2[data2$powercoup==1,], family=binomial(link=logit))

three <- glm(elx~factor(country)+t+t2+t3+pop+junior+post90+Oil+preprotest+gdppc+chgdppc+aidlag*predem, data=data3[data3$powercoup==1,], family=binomial(link=logit))

four <- glm(elx~factor(country)+t+t2+t3+pop+junior+post90+Oil+preprotest+gdppc+chgdppc+duration2+aidlag*predem, data=data3[data3$powercoup==1,], family=binomial(link=logit))

stargazer(one, two, three, four)



## Table 5: Null Effects Using Alternative Measures of International Leverage

one <- glm(elex~t+t2+t3+gdp+chgdp+gdplag+prchgdp+pop+junior+post90+preprotest+Oil+econaidlag*predem, data=data[data$powercoup==1,], family=binomial(link=logit))
two <- glm(elex~t+t2+t3+gdp+chgdp+gdplag+prchgdp+pop+junior+post90+preprotest+Oil+demx_trade2*predem, data=data[data$powercoup==1,], family=binomial(link=logit))
three <- glm(elex~t+t2+t3+gdp+chgdp+gdplag+prchgdp+pop+junior+post90+preprotest+Oil+demx_alliance*predem, data=data[data$powercoup==1,], family=binomial(link=logit))
four <- glm(elex~t+t2+t3+gdp+chgdp+gdplag+prchgdp+pop+junior+post90+preprotest+Oil+regioneax*predem, data=data[data$powercoup==1,], family=binomial(link=logit))

stargazer(one, two, three, four, single.row=TRUE)


# Table 6: Controlling for Alternative Measures of Leverage

one <- glm(elex~t+t2+t3+gdp+chgdp+gdplag+prchgdp+pop+junior+post90+preprotest+Oil+econaidlag+aidlag*predem, data=data[data$powercoup==1,], family=binomial(link=logit))
two <- glm(elex~t+t2+t3+gdp+chgdp+gdplag+prchgdp+pop+junior+post90+preprotest+Oil+demx_trade2+aidlag*predem, data=data[data$powercoup==1,], family=binomial(link=logit))
three <- glm(elex~t+t2+t3+gdp+chgdp+gdplag+prchgdp+pop+junior+post90+preprotest+Oil+demx_alliance+aidlag*predem, data=data[data$powercoup==1,], family=binomial(link=logit))
four <- glm(elex~t+t2+t3+gdp+chgdp+gdplag+prchgdp+pop+junior+post90+preprotest+Oil+regioneax+aidlag*predem, data=data[data$powercoup==1,], family=binomial(link=logit))
five <- glm(elex~t+t2+t3+gdp+chgdp+gdplag+prchgdp+pop+junior+post90+preprotest+Oil+econaidlag+demx_trade2+demx_alliance+regioneax+aidlag*predem, data=data[data$powercoup==1,], family=binomial(link=logit))

stargazer(one, two, three, four, five, single.row=TRUE)




## Table 7: Continuous Measures of Military Aid

one <- glm(elex~t+t2+t3+predem*log(aidper+0.00001)+gdp+chgdp+gdplag+prchgdp+pop+junior+post90+Oil+preprotest, data=data[data$powercoup==1,], family=binomial(link=logit))

two <- glm(elex~t+t2+t3+predem*log(aidex+0.00001)+gdp+chgdp+gdplag+prchgdp+pop+junior+post90+Oil+preprotest, data=data[data$powercoup==1 & data$aidex<1000,], family=binomial(link=logit))

stargazer(one, two, single.row=TRUE)




## Table 8: Results are Not Driven by Post-Cold War period

one <- glm(elex~t+t2+t3+gdp+chgdp+gdplag+prchgdp+pop+junior+Oil+preprotest+aidlag*predem*post90, data=data[data$powercoup==1,], family=binomial(link=logit))

two <- glm(elex~aidlag*predem, data=data[data$powercoup==1 & data$post90==0,], family=binomial(link=logit))

three <- glm(elex~aidlag*predem, data=data[data$powercoup==1 & data$post90==1,], family=binomial(link=logit))

stargazer(one, two, three, single.row=TRUE)



###############
## by decade ##
###############

## 1946-1959
data.summary <- tapply(data$elex[data$powercoup==1 & data$year>1945 & data$year<1960], data$IV[data$powercoup==1 & data$year>1945 & data$year<1960], mean, na.rm=TRUE)
barplot <- barplot(data.summary, ylim=c(0,1), ylab="Proportion Holding Elections", cex.names=0.9, col="DarkBlue", main="Power-Seeking Coups, 1946-1959 (N=22)", names=c("Pr. Autocratic\n + Mil Aid", "Pr. Autocratic\n + No Mil Aid", "Pr. Democratic\n + Mil Aid", "Pr. Democratic\n + No Mil Aid"))
text(x=t(barplot), y=data.summary+0.06, labels=format(round(data.summary, 2),2), pos=2, offset=-0.8)

## 1960-1969
data.summary <- tapply(data$elex[data$powercoup==1 & data$year>1959 & data$year<1970], data$IV[data$powercoup==1 & data$year>1959 & data$year<1970], mean, na.rm=TRUE)
barplot <- barplot(data.summary, ylim=c(0,1), ylab="Proportion Holding Elections", cex.names=0.9, col="DarkBlue", main="Power-Seeking Coups, 1960-1969 (N=56)", names=c("Pr. Autocratic\n + Mil Aid", "Pr. Autocratic\n + No Mil Aid", "Pr. Democratic\n + Mil Aid", "Pr. Democratic\n + No Mil Aid"))
text(x=t(barplot), y=data.summary+0.06, labels=format(round(data.summary, 2),2), pos=2, offset=-0.8)

## 1970-1979
data.summary <- tapply(data$elex[data$powercoup==1 & data$year>1969 & data$year<1980], data$IV[data$powercoup==1 & data$year>1969 & data$year<1980], mean, na.rm=TRUE)
barplot <- barplot(data.summary, ylim=c(0,1), ylab="Proportion Holding Elections", cex.names=0.9, col="DarkBlue", main="Power-Seeking Coups, 1970-1979 (N=45)", names=c("Pr. Autocratic\n + Mil Aid", "Pr. Autocratic\n + No Mil Aid", "Pr. Democratic\n + Mil Aid", "Pr. Democratic\n + No Mil Aid"))
text(x=t(barplot), y=data.summary+0.06, labels=format(round(data.summary, 2),2), pos=2, offset=-0.8)

## 1980-1990
data.summary <- tapply(data$elex[data$powercoup==1 & data$year>1979 & data$year<1991], data$IV[data$powercoup==1 & data$year>1979 & data$year<1991], mean, na.rm=TRUE)
barplot <- barplot(data.summary, ylim=c(0,1.1), ylab="Proportion Holding Elections", cex.names=0.9, col="DarkBlue", main="Power-Seeking Coups, 1980-1990 (N=34)", names=c("Pr. Autocratic\n + Mil Aid", "Pr. Autocratic\n + No Mil Aid", "Pr. Democratic\n + Mil Aid", "Pr. Democratic\n + No Mil Aid"))
text(x=t(barplot), y=data.summary+0.06, labels=format(round(data.summary, 2),2), pos=2, offset=-0.8)

## 1991-
data.summary <- tapply(data$elex[data$powercoup==1 & data$year>1990], data$IV[data$powercoup==1 & data$year>1990], mean, na.rm=TRUE)
barplot <- barplot(data.summary, ylim=c(0,1.1), ylab="Proportion Holding Elections", cex.names=0.9, col="DarkBlue", main="Power-Seeking Coups, 1991-2014 (N=28)", names=c("Pr. Autocratic\n + Mil Aid", "Pr. Autocratic\n + No Mil Aid", "Pr. Democratic\n + Mil Aid", "Pr. Democratic\n + No Mil Aid"))
text(x=t(barplot), y=data.summary+0.06, labels=format(round(data.summary, 2),2), pos=2, offset=-0.8)






## Table 9: Excluding Short-Lived Coups (logit)

one <- glm(elex~t+t2+t3+gdp+chgdp+gdplag+prchgdp+pop+junior+post90+Oil+preprotest+aidlag*predem, data=data[data$powercoup==1 & data$duration>5,], family=binomial(link=logit))

two <- glm(elex~t+t2+t3+gdp+chgdp+gdplag+prchgdp+pop+junior+post90+Oil+preprotest+aidlag*predem, data=data[data$powercoup==1 & data$duration>11,], family=binomial(link=logit))

three <- glm(elex~t+t2+t3+gdp+chgdp+gdplag+prchgdp+pop+junior+post90+Oil+preprotest+aidlag*predem, data=data[data$powercoup==1 & data$duration>17,], family=binomial(link=logit))

four <- glm(elex~t+t2+t3+gdp+chgdp+gdplag+prchgdp+pop+junior+post90+Oil+preprotest+aidlag*predem, data=data[data$powercoup==1 & data$duration>23,], family=binomial(link=logit))

five <- glm(elex~t+t2+t3+gdp+chgdp+gdplag+prchgdp+pop+junior+post90+Oil+preprotest+aidlag*predem, data=data[data$powercoup==1 & data$duration>29,], family=binomial(link=logit))

six <- glm(elex~gdp+chgdp+gdplag+prchgdp+pop+junior+post90+Oil+preprotest+duration+aidlag*predem, data=data[data$powercoup==1,], family=binomial(link=logit))

stargazer(one, two, three, four, five, six, single.row=T)





## Figure 4: Time to First Election

hist(data$elextime[data$powercoup==1], breaks=12, freq=F, main="Time to Election (N=98)",
     xlab="Years after Coup")



## Table 10: Excluding Late Elections

data$elex2 <- ifelse(data$elex==1 & data$elextime<6.9, 1, 0)
one <- glm(elex2~t+t2+t3+gdp+chgdp+gdplag+prchgdp+pop+junior+post90+Oil+preprotest+aidlag*predem, data=data[data$powercoup==1,], family=binomial(link=logit))

data$elex2 <- ifelse(data$elex==1 & data$elextime<5.9, 1, 0)
two <- glm(elex2~t+t2+t3+gdp+chgdp+gdplag+prchgdp+pop+junior+post90+Oil+preprotest+aidlag*predem, data=data[data$powercoup==1,], family=binomial(link=logit))

data$elex2 <- ifelse(data$elex==1 & data$elextime<4.9, 1, 0)
three <- glm(elex2~t+t2+t3+gdp+chgdp+gdplag+prchgdp+pop+junior+post90+Oil+preprotest+aidlag*predem, data=data[data$powercoup==1,], family=binomial(link=logit))

stargazer(one, two, three, single.row=T)




## Table 11: Include non power seeking coups

one <- glm(elex~t+t2+t3+aidlag*predem, data=data, family=binomial(link=logit))
two <- glm(elex~t+t2+t3+aidlag*predem+gdp+chgdp+gdplag+prchgdp+pop+junior+post90+Oil+preprotest, data=data, family=binomial(link=logit))

stargazer(one, two, single.row=TRUE)



## Table 12: Sham or Competitive Elections

one <- glm(autelex~t+t2+t3+aidlag*predem, data=data[data$powercoup==1,], family=binomial(link=logit))

two <- glm(autelex~t+t2+t3+aidlag*predem+gdp+chgdp+gdplag+prchgdp+pop+junior+post90+Oil+preprotest, data=data[data$powercoup==1,], family=binomial(link=logit))

three <- glm(compfirst~t+t2+t3+aidlag*predem, data=data[data$powercoup==1,], family=binomial(link=logit))

four <- glm(compfirst~t+t2+t3+aidlag*predem+gdp+chgdp+gdplag+prchgdp+pop+junior+post90+Oil+preprotest, data=data[data$powercoup==1,], family=binomial(link=logit))

stargazer(one, two, three, four, single.row=TRUE)




## Table 13: Excluding Short-Lived Coups (Weibull)

one <- survreg(Surv(duration+1, event=censor)~aidlag*predem+elex_lost+gdp+chgdp+gdplag+prchgdp+pop+junior+post90+Oil+preprotest+elex, data=data[data$powercoup==1 & data$duration>11,], dist="weibull")

two <- survreg(Surv(duration+1, event=censor)~aidlag*predem+elex_lost+gdp+chgdp+gdplag+prchgdp+pop+junior+post90+Oil+preprotest+elex, data=data[data$powercoup==1 & data$duration>23,], dist="weibull")

three <- survreg(Surv(duration+1, event=censor)~aidlag*predem+elex_lost+gdp+chgdp+gdplag+prchgdp+pop+junior+post90+Oil+preprotest+elex, data=data[data$powercoup==1 & data$duration>59,], dist="weibull")

four <- survreg(Surv(duration+1, event=censor)~aidlag*predem+elex_lost+gdp+chgdp+gdplag+prchgdp+pop+junior+post90+Oil+preprotest+elex, data=data[data$powercoup==1 & data$duration>119,], dist="weibull")

stargazer(one,two,three,four,single.row=TRUE)


